package com.kingint.web.admin.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.web.sys.service.UmsMemberService;
import com.kingint.web.util.validateUtil.ValidList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasRoomService;
import com.kingint.mbg.bas.entity.BasRoom;

import java.util.Date;


/**
 * @描述：房间信息控制类
 * @作者: wxh
 * @日期: 2020-12-23
 */
@RestController
@RequestMapping("/basRoom")
@Api(value = "BasRoomController", description = "房间信息")
@CrossOrigin
public class BasRoomController{

    @Autowired
    public BasRoomService basRoomService;
    @Autowired
    public UmsMemberService umsMemberService;


    @ApiOperation(value = "添加房间信息")
    @PostMapping("/create")
    // @PreAuthorize("hasAuthority('basRoom:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasRoom basRoom){
        Integer userHotel = umsMemberService.getUserHotel();
        basRoom.setHotelId(userHotel);
        basRoom.setCreateTime(new Date());
        basRoom.setStatus(0);
        Boolean bool=basRoomService.saveOrUpdate(basRoom);
        if(bool){
            return K.ok().data(basRoom.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "批量添加房间信息")
    @PostMapping("/insertList")
    //@PreAuthorize("hasAuthority('basRoom:insertList')")
    public Object create(@RequestBody @Validated(Insert.class)  ValidList<BasRoom> basRoom){
        Boolean bool=basRoomService.saveOrUpdateBatch(basRoom);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新房间信息")
    @PutMapping("/update")
    // @PreAuthorize("hasAuthority('basRoom:update')")
    public Object update(@Validated(Update.class) @RequestBody BasRoom basRoom){
        Boolean bool= basRoomService.updateById(basRoom);
        if(bool){
            return K.ok().data(basRoom.getId());
        }else{
            return K.error();
        }
    }


    @ApiOperation(value = "删除房间信息")
    @DeleteMapping("/delete/{id}")
    //  @PreAuthorize("hasAuthority('basRoom:delete')")
    public Object delete(@PathVariable("id") Integer id){
        Boolean bool= basRoomService.removeById(id);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "根据房间信息名称分页获取房间信息列表")
    @GetMapping("/list")
    //  @PreAuthorize("hasAuthority('basRoom:list')")
    public Object getList(BasRoom basRoom,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        basRoom.setHotelId(umsMemberService.getUserHotel());

        Page<BasRoom> page = new Page<>(pageNum, pageSize);
        return basRoomService.getpage(page,basRoom);

    }

    @ApiOperation(value = "根据编号查询房间信息信息")
    @GetMapping("/{id}")
    // @PreAuthorize("hasAuthority('basRoom:get')")
    public Object getItem(@PathVariable("id") Long id){
        BasRoom basRoom=basRoomService.getById(id);
        return K.ok().data(basRoom);
    }
}